package com.ruoyi.mobile.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mobile.domain.NoticeUser;
import com.ruoyi.mobile.mapper.NoticeUserMapper;
import com.ruoyi.mobile.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mobile.mapper.NoticeMapper;
import com.ruoyi.mobile.domain.Notice;
import com.ruoyi.mobile.service.INoticeService;

/**
 * 系统通知Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-19
 */
@Service
public class NoticeServiceImpl implements INoticeService 
{
    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private NoticeUserMapper noticeUserMapper;

    /**
     * 查询系统通知
     * 
     * @param id 系统通知主键
     * @return 系统通知
     */
    @Override
    public Notice selectNoticeById(Integer id)
    {
        return noticeMapper.selectNoticeById(id);
    }

    /**
     * 查询系统通知列表
     * 
     * @param notice 系统通知
     * @return 系统通知
     */
    @Override
    public List<Notice> selectNoticeList(Notice notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增系统通知
     * 
     * @param notice 系统通知
     * @return 结果
     */
    @Override
    public int insertNotice(Notice notice)
    {
        notice.setCreateBy(SecurityUtils.getLoginUser().getUserId().toString());
        notice.setCreateTime(DateUtils.getNowDate());
        noticeMapper.insertNotice(notice);
        //发送给所有用户
        List<Integer> userIdList = userMapper.selectUserIdList();
        List<NoticeUser> noticeUsers = new ArrayList<>();
        for (Integer userId : userIdList) {
            NoticeUser noticeUser = new NoticeUser();
            noticeUser.setUserId(userId);
            noticeUser.setNoticeId(notice.getId());
            noticeUser.setCreateTime(new Date());
            noticeUsers.add(noticeUser);
        }
        noticeUserMapper.insertList(noticeUsers);
        return 1;
    }

    /**
     * 修改系统通知
     * 
     * @param notice 系统通知
     * @return 结果
     */
    @Override
    public int updateNotice(Notice notice)
    {
        notice.setUpdateBy(SecurityUtils.getLoginUser().getUserId().toString());
        notice.setUpdateTime(DateUtils.getNowDate());
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 批量删除系统通知
     * 
     * @param ids 需要删除的系统通知主键
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Integer[] ids)
    {
        return noticeMapper.deleteNoticeByIds(ids);
    }

    /**
     * 删除系统通知信息
     * 
     * @param id 系统通知主键
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Integer id)
    {
        return noticeMapper.deleteNoticeById(id);
    }
}
